package com.struts.action.product;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.hibernate.pojo.Product;
import com.spring.service.product.ProductService;
import com.struts.form.ProductForm;

public class ProductAction extends DispatchAction
{
	private ProductService productService=null;
	
	public ProductService getProductService() {
		return productService;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	/**
	 * find product list
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward findProducts(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		int page=0;
		int productleibieID = 0;
		if(request.getParameter("page")!=null)
		{
			page=Integer.parseInt(request.getParameter("page"));
		}
		if(request.getParameter("productleibieID")!=null && request.getParameter("productleibieID")!="")
		{
			productleibieID=Integer.parseInt(request.getParameter("productleibieID"));
		}
		System.out.println(productleibieID);
		Map hashmap=productService.findProducts(page,productleibieID);
		request.setAttribute("page_content", hashmap.get("page_content"));
		request.setAttribute("product_list", hashmap.get("product_list"));
		return mapping.findForward("product_list");
	}
	
	
	/**
	 * save product
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward saveProduct(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		ProductForm productform=(ProductForm)form;
		Long product_id=productService.saveProduct(productform);
		if(product_id!=0)
		{
			String path = mapping.findForward("saveone").getPath();
			ActionForward forward= new ActionForward(path+"?page="+request.getParameter("page")+"&productleibieID="+request.getParameter("categry_id")+"&product_id="+product_id);
			forward.setRedirect(true);
			return forward;
		}
		else
			return mapping.findForward("fail");
	}
	
	
	public ActionForward updateProduct(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		ProductForm productform=(ProductForm)form;
		Map product=productService.updateProduct(productform);
		request.setAttribute("product_id",productform.getId());
		request.setAttribute("product", product);
		request.setAttribute("page", request.getParameter("page"));
		request.setAttribute("productleibieID", request.getParameter("categry_id"));
		System.out.println(request.getParameter("categry_id"));
		return mapping.findForward("editone");
	}
	
	
	public ActionForward updateProductView(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		Long id=Long.parseLong(request.getParameter("id"));
		List list=productService.updateProductView(id);
		request.setAttribute("update_product_view",list.get(0));
		request.setAttribute("product_content",list.get(1));
		request.setAttribute("categry_id",list.get(2));
		request.setAttribute("page", request.getParameter("page"));
		return mapping.findForward("update_product_view");
	}
	
	
	
	/**
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward updateProductAudit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		Long id=Long.parseLong(request.getParameter("id"));
		Byte audit=Byte.parseByte(request.getParameter("audit"));
		if(audit==0)
			audit=1;
		else
			audit=0;
		ProductForm productform=new ProductForm();
		productform.setId(id);
		productform.setProduct_audit(audit);
		if(productService.updateProductAudit(productform))
		{
			 String path = mapping.findForward("success").getPath();
			 ActionForward forward= new ActionForward(path+"&page="+request.getParameter("page")+"&productleibieID="+request.getParameter("productleibieID"));
			 forward.setRedirect(true);
			 return forward;
		}
		else
			return mapping.findForward("fail");
	}
	
	
	
	public ActionForward deleteProduct(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		Long id=Long.parseLong(request.getParameter("id"));
		if(productService.deleteProduct(id))
		{
			 String path = mapping.findForward("success").getPath();
			 ActionForward forward= new ActionForward(path+"&page="+request.getParameter("page")+"&productleibieID="+request.getParameter("productleibieID"));
			 forward.setRedirect(true);
			 return forward;
		}
		else
		{
			return mapping.findForward("fail");
		}
	}
	
}
